#ifndef FanMotor_H
#define FanMotor_H

#define SPEEDKEEP 0x00
#define SPEEDDOWN 0x01
#define SPEEDUP 0x02

#define FANERR0 0x00 // 正常运行
#define FANERR1 0x01 // 过零200us斩波15秒
#define FANERR2 0x02 // 停5秒
#define FANERR3 0x03 // 全速启动15秒
#define FANERR 0x04  // 风机故障

#ifndef PG_MOTOR
#define DC_MOTOR_DUTY_NUM 4 /* 转速调节周期按照1F3F计数，实际PWM控制需要乘一个系数 */
#endif

#ifdef PG_MOTOR
#define BACKCOUNT 4
#else
#define BACKCOUNT 16
#endif

#ifdef FanMotor_Variable
#define FanMotor_Extern

static U8 SpeedAdjStat;
static U16 SpeedAdjValue;

static U16 SpeedBackCount1[BACKCOUNT];
static U16 SpeedBackCount2[BACKCOUNT];

static U16 SpeedSubFreq;
static U16 SpeedSubFreqOld;

static U16 SpeedBackSumCount1;
static U16 SpeedBackSumCount2;
static U16 SpeedBackSubCount;


static U16 SpeedSubValue;

static U16 SpeedSubValueOld;

#else
#define FanMotor_Extern extern
#endif

FanMotor_Extern U8 fan_motor_num; // 风机个数，默认个数为1；

FanMotor_Extern U8 stat_fanErr;

typedef struct
{
    U8 adj; /* 风速调节计时 */
    U8 act;
    U8 chg;

    U8 ZeroErr;
    U8 ZeroErrQT;
    U8 ZeroCheck;

    U16 speedBack1;
    U16 speedBack2;

    U16 keepHumMin;
    U16 keepHumSec;
} fan_timer_typeDef;


extern fan_timer_typeDef fan_timer;



FanMotor_Extern U16 speedActDuty;
FanMotor_Extern U16 speedActWidth;
FanMotor_Extern U16 speedNowValue1;
FanMotor_Extern U16 speedNowValue2;
FanMotor_Extern U16 speedSetValue;
FanMotor_Extern U16 speedSetValueRxd;

FanMotor_Extern U8 acfan_level;

FanMotor_Extern U16 ZeroRepair;
FanMotor_Extern U16 FanPulseMin;
FanMotor_Extern U16 FanPulseMax;
FanMotor_Extern U16 FanPulseAct;

FanMotor_Extern ram_byte PGMotorFlag_u;
#define f_FanBack1 PGMotorFlag_u.bit.b0
#define f_FanBack2 PGMotorFlag_u.bit.b1
#define f_FanStartOver PGMotorFlag_u.bit.b2
#define f_Power60Hz PGMotorFlag_u.bit.b3
#define f_PowerHzEnd PGMotorFlag_u.bit.b4
#define f_ZeroRepair PGMotorFlag_u.bit.b5
#define f_KeepHumToFan PGMotorFlag_u.bit.b6

void SpeedBackIntp1(void);
void SpeedBackIntp2(void);
void Fan_Ctrl_Timer1_Intp(void);
void Fan_Ctrl_Timer2_Intp(void);
void ZeroCheckIntp(void);
void PG_FanDriver(void);
void PG_PowerHzJudge(void);
void PG_ZeroErrCheck(void);
void InFanAct(void);
void InFanAdj(void);
void SpeedCheckErr(void);
void keepHumFanAct(void);
void FanMotorTime(void);
#endif
